<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('payment_methods', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string("label")->nullable();
        });

        $this->initPaymentMethods();

        Schema::create('order_statuses', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string("label")->nullable();
        });

        $this->initOrderStatus();

        Schema::create('orders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('customer_id');
            $table->string('order_number')->unique();
            $table->longText("description")->nullable();
            $table->date('expected_rental_out_date')->comment('预期借出日期');
            $table->date('real_rental_out_date')->nullable()->comment('实际借出日期');
            $table->date('expected_return_date')->comment('预期归还日期');
            $table->date('real_return_date')->nullable()->comment('实际归还时间');
            $table->date("expected_ship_date")->nullable()->comment('预期物流送出时间');
            $table->date("real_ship_date")->nullable()->comment('实际物流送出时间');
            $table->unsignedInteger('status_id');
            $table->unsignedInteger("payment_method_id");
            $table->boolean("is_paid")->default(false);
            $table->date("paid_at")->nullable()->comment('支付时间');
            $table->decimal("net_amount")->default(0)->comment('需要支付的费用');
            $table->decimal("paid_amount")->default(0)->comment('已支付费用');
            $table->decimal("payable_amount")->default(0)->comment('待支付费用');
            $table->string("ship_address")->nullable();
            $table->string("identification_number")->nullable()->comment('物流标识符');
            $table->string("location")->nullable()->comment('仓库地址');
            $table->double("discount")->default(0)->comment('折扣');
            $table->unsignedBigInteger("created_by");
            $table->unsignedBigInteger("assigned_to")->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('status_id')->references('id')->on('order_statuses')->onDelete('cascade');
            $table->foreign('payment_method_id')->references('id')->on('payment_methods')->onDelete('cascade');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
        });

        Schema::create('order_items', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('order_id');
            $table->string("name")->comment("资产名称");
            $table->unsignedBigInteger("rentalable_id");
            $table->string("rentalable_type");
            $table->integer("quantity")->default(1)->comment("数量");
            $table->decimal("unit_price")->default(1)->comment("单价");
            $table->decimal("amount")->default(0)->comment("价格");
            $table->dateTime("rental_out_at")->nullable()->comment("出库时间");
            $table->dateTime("return_at")->nullable()->comment("入库时间");
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create("order_extra_fees", function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string("name")->comment("支付名称");
            $table->text("description")->nullable();
            $table->integer("quantity")->default(1)->comment("数量");
            $table->decimal("unit_price")->default(0)->comment("单价");
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {


        Schema::dropIfExists('order_extra_fees');
        Schema::dropIfExists('order_items');
        Schema::dropIfExists('orders');
        Schema::dropIfExists('order_statuses');
        Schema::dropIfExists('payment_methods');

    }

    private function initOrderStatus()
    {
        $status = [["preview", "预览"], ["active", "生效"], ["completed", "完成"], ["canceled", "取消"]];
        foreach ($status as $s) {
            \App\Models\Order\OrderStatus::create([
                "name" => $s[0],
                "label" => $s[1]
            ]);
        }
    }

    private function initPaymentMethods()
    {
        $status = [["支付宝", "支付宝"], ["银行转账", "银行转账"], ["现金", "现金"], ["微信", "微信"]];
        foreach ($status as $s) {
            \App\Models\Order\PaymentMethod::create([
                "name" => $s[0],
                "label" => $s[1]
            ]);
        }
    }
}
